package com.vogue.supplymall.entmanagement.domain.service.impl;

import com.vogue.ssm.database.dao.BaseDao;
import com.vogue.supplymall.common.domain.dto.admin.LicenceSearchInfo;
import com.vogue.supplymall.entmanagement.domain.dao.LicenceApplyDetailDao;
import com.vogue.supplymall.entmanagement.domain.model.LicenceApplyDetail;
import com.vogue.supplymall.entmanagement.domain.service.LicenceApplyDetailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * The type Licence apply detail service.
 *
 * @aouthor: zhangliang
 * @date: Create in 2018-04-16
 */
@Service("licenceApplyDetailService")
public class LicenceApplyDetailServiceImpl extends BaseServiceImpl<LicenceApplyDetail, Long> implements LicenceApplyDetailService {
    @Resource
    private LicenceApplyDetailDao licenceApplyDetailDao;

    /**
     * Instantiates a new Licence apply detail service.
     *
     * @param baseDao the base dao
     */
    public LicenceApplyDetailServiceImpl(BaseDao<LicenceApplyDetail, Long> baseDao) {
        super(baseDao);
    }

    @Override
    public List<Map<String, Object>> findOverdueLicence(LicenceSearchInfo searchInfo) {
        StringBuilder sql = new StringBuilder().append("select licencetypeid,begindate,enddate,url from licence_apply_detail where shopid = #{shopid} ");
        if (null != searchInfo.getLicenceTypeId()) {
            sql.append(" AND licencetypeid = #{licencetypeid} ");
        }
        if (StringUtils.isNotEmpty(searchInfo.getBegindate())) {
            sql.append(" AND begindate >= #{begindate} ");
        }

        if (StringUtils.isNotEmpty(searchInfo.getEnddate())) {
            sql.append(" AND enddate <= #{enddate} ");
        } else {
            sql.append(" and enddate <= NOW()  ");
        }

        sql.append(" limit #{start},#{length} ");
        Map<String, Object> para = new HashMap<>();
        para.put("shopid", searchInfo.getShopId());
        para.put("licencetypeid", searchInfo.getLicenceTypeId());
        para.put("begindate", searchInfo.getBegindate());
        para.put("enddate", searchInfo.getEnddate());
        para.put("start", searchInfo.getStart());
        para.put("length", searchInfo.getLength());
        return licenceApplyDetailDao.query0(sql.toString(), para);
    }

    @Override
    public Long findOverdueLicenceCount(LicenceSearchInfo searchInfo) {
        StringBuilder sql = new StringBuilder().append("select count(id) count from licence_apply_detail where shopid = #{shopid} ");
        if (null != searchInfo.getLicenceTypeId()) {
            sql.append(" AND licencetypeid = #{licencetypeid} ");
        }
        if (StringUtils.isNotEmpty(searchInfo.getBegindate())) {
            sql.append(" AND begindate >= #{begindate} ");
        }

        if (StringUtils.isNotEmpty(searchInfo.getEnddate())) {
            sql.append(" AND enddate <= #{enddate} ");
        } else {
            sql.append(" and enddate <= NOW()  ");
        }

        Map<String, Object> para = new HashMap<>();
        para.put("shopid", searchInfo.getShopId());
        para.put("licencetypeid", searchInfo.getLicenceTypeId());
        para.put("begindate", searchInfo.getBegindate());
        para.put("enddate", searchInfo.getEnddate());
        List<Map<String, Object>> lists = licenceApplyDetailDao.query0(sql.toString(), para);
        return lists.size() > 0 ? new Long(lists.get(0).get("count").toString()) : 0L;
    }

    @Override
    public List<Map<String, Object>> findByApplyId(Long applyId) {
        String sql = "select * from licence_apply_detail where applyid = #{applyid}";
        Map<String, Object> para = new HashMap<>();
        para.put("applyid", applyId);
        return licenceApplyDetailDao.query0(sql, para);
    }
}
